package com.xsmart.order.dao;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 *
 * @author Administrator
 * @date 2017/12/11
 */
@Component
public interface GoodsManageDao {
    /**
     * 查询商品
     * @param goodsId
     * @return
     */
    @Select("<script>" +
            "SELECT t.goods_id, t.parent_goods_id,t.cat_id,t.user_id,t.goods_sn,t.goods_name,t.click_count,t.brand_id," +
            "t.goods_number,t.goods_weight,t.market_price,t.cost_price,t.supply_price,t.shop_price,t.promote_price," +
            "DATE_FORMAT(t.promote_start_date,'%Y-%m-%d %T')promote_start_date," +
            "DATE_FORMAT(t.promote_end_date,'%Y-%m-%d %T')promote_end_date," +
            "t.warn_number,t.keywords,t.goods_brief,t.goods_desc,t.goods_thumb,t.goods_img,t.is_real,t.is_on_sale," +
            "t.is_alone_sale,t.add_time,t.sort_order,t.is_delete,t.is_best,t.is_new,t.is_hot,t.goods_type,t.seller_note," +
            "<![CDATA[(case when t.is_promote=1 and t.promote_start_date <= NOW() and t.promote_end_date >= NOW() then t.promote_price ELSE t.shop_price END)]]>  goods_price,  " +
            "t.give_integral,t.rank_integral,t.is_xiangou,t.mod_count, " +
            "DATE_FORMAT(t.xiangou_start_date,'%Y-%m-%d %T')xiangou_start_date," +
            "DATE_FORMAT(t.xiangou_end_date,'%Y-%m-%d %T')xiangou_end_date," +
            "t.xiangou_num,t.goods_tag,t.measure_unit,t.from_seller_id," +
            "b.brand_name,c.cat_name,g.cat_name typeName ,DATE_FORMAT(t.add_time,'%Y-%m-%d %T') add_times " +
            " FROM goods t " +
            " LEFT JOIN brand b on t.brand_id =b.brand_id " +
            " LEFT JOIN category c on c.cat_id=t.cat_id " +
            " LEFT JOIN goods_type g on t.goods_type=g.cat_id " +
            "<where>" +
            " t.goods_id=#{goodsId}" +
            "</where>" +
            "</script>")
    Map<String,Object> queryGoodsInfo(@Param("goodsId") Integer goodsId);

    /**
     * 更新库存
     * @param goodsId 商品id
     * @param goodsNumber 使用数量
     * @return 影响记录数
     */
    @Update("update goods set goods_number=(case when goods_number>#{goodsNumber} then goods_number-#{goodsNumber} else 0 end) where goods_id=#{goodsId}")
    int updateGoodsNumber(@Param("goodsId") Integer goodsId, @Param("goodsNumber") int goodsNumber);

    /**
     * 取消订单后更新库存
     * @return 影响记录数
     */
    @Update("update goods set goods_number=(goods_number+#{goods_number}) where goods_id=#{goods_id}")
    int updateGoodsNum(Map goodsMap);
}
